//
//  YBOneClickLoginView.swift
//  Moonlight
//
//  Created by Penlon Kim on 2024/1/1.
//  Copyright © 2024 Moonlight Game Streaming Project. All rights reserved.
//

import UIKit
import SnapKit

protocol YBOneClickLoginViewDelegate: NSObjectProtocol {
    func oneClickLoginViewDidClick(_ btnType: YBOneClickLoginView.BtnType)
}

class YBOneClickLoginView: UIView {
    
    enum BtnType {
        case oneClickLogin
        case register
        case phonePwdLogin
    }
    
    weak var delegate: YBOneClickLoginViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubviews([imgView, titleLabel, phoneNumberLabel, desLabel, oneClickLoginBtn, registerBtn, phonePwdLoginBtn, tipsTextView, checkBtn])
        
        imgView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(kNavigationBarHeight*1.8).priority(.high)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(imgView.snp.bottom).offset(36)
            make.height.equalTo(22)
        }
        
        phoneNumberLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(22)
            make.height.equalTo(27)
        }
        
        desLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(phoneNumberLabel.snp.bottom).offset(18)
            make.height.equalTo(13)
        }
        
        oneClickLoginBtn.snp.makeConstraints { make in
            make.top.equalTo(desLabel.snp.bottom).offset(56)
            make.left.right.equalToSuperview().inset(27)
            make.height.equalTo(45)
            make.centerX.equalToSuperview()
        }
        
        registerBtn.snp.makeConstraints { make in
            make.top.equalTo(oneClickLoginBtn.snp.bottom).offset(12)
            make.right.equalTo(oneClickLoginBtn)
        }
        
        phonePwdLoginBtn.snp.makeConstraints { make in
            make.left.equalTo(oneClickLoginBtn)
            make.centerY.equalTo(registerBtn)
        }
        
        checkBtn.snp.makeConstraints { make in
            make.size.equalTo(15)
            make.left.equalTo(29)
            make.top.equalTo(phonePwdLoginBtn.snp.bottom).offset(40)
            make.bottom.lessThanOrEqualTo(-kBottomSafeHeight-offset30)
        }
        
        tipsTextView.snp.makeConstraints { make in
            make.centerY.equalTo(checkBtn)
            make.left.equalTo(checkBtn.snp.right).offset(6)
            make.right.equalTo(-25)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - lazy
    private lazy var imgView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "icon_login_kkygame"))
        iv.contentMode = .scaleAspectFit
        iv.clipsToBounds = true
        return iv
    }()
    
    private lazy var titleLabel = self.bk_addLabel(text: "一键登录", font: .systemFont(ofSize: 18, weight: .bold), bgColor: .clear, textColor: .lightBlackDarkWhite, align: .center)
    
    private lazy var phoneNumberLabel = self.bk_addLabel(text: "195****9826", font: .systemFont(ofSize: 24, weight: .bold), bgColor: .clear, textColor: .lightBlackDarkWhite, align: .center)
    
    private lazy var desLabel = self.bk_addLabel(text: "认证服务由中国移动提供", font: .systemFont(ofSize: 11), bgColor: .clear, textColor: ColorDef.gray153, align: .center)
    
    private lazy var oneClickLoginBtn: UIButton = {
        let btn = self.bk_addButton(type: .custom, title: "本机号码一键登录", font: .systemFont(ofSize: 18), bgColor: ColorDef.dark27, titleColor: .white, radius: 8)
        btn.bk_addShadowStyleWith(cornerRadius: 8, corners: .allCorners)
        btn.bk_addTarget { [unowned self] sender in
            self.delegate?.oneClickLoginViewDidClick(.oneClickLogin)
        }
        return btn
    }()
    
    private lazy var phonePwdLoginBtn: UIButton = {
        let btn = self.bk_addButton(title: "账号密码登录", font: .systemFont(ofSize: 13), bgColor: .clear, titleColor: ColorDef.gray153)
        btn.bk_setEnlargeEdge(10)
        btn.bk_addTarget { [unowned self] sender in
            self.delegate?.oneClickLoginViewDidClick(.phonePwdLogin)
        }
        return btn
    }()
    
    private lazy var registerBtn: UIButton = {
        let btn = self.bk_addButton(title: "去注册", font: .systemFont(ofSize: 13), bgColor: .clear, titleColor: ColorDef.gray153)
        btn.bk_setEnlargeEdge(10)
        btn.bk_addTarget { [unowned self] sender in
            self.delegate?.oneClickLoginViewDidClick(.register)
        }
        return btn
    }()
    
    lazy var checkBtn: UIButton = {
        let btn = self.bk_addImgSelectButton(iconNormal: "icon_check_normal", iconSelect: "icon_check_select")
        btn.bk_setEnlargeEdge(10)
        btn.bk_addTarget { [unowned self] (sender) in
            sender.isSelected = !sender.isSelected
        }
        return btn
    }()
    
    /// 用户协议
    private lazy var tipsTextView: UITextView = {
        let textV = UITextView()
        textV.backgroundColor = .clear
        textV.textAlignment = .left
        textV.isEditable = false
        textV.isScrollEnabled = false
        textV.delegate = self
        textV.linkTextAttributes = [.foregroundColor: UIColor.lightBlack58DarkLight230, .font: UIFont.systemFont(ofSize: 12, weight: .semibold)]
        textV.attributedText = self.reloadTipsTextAttr()
        return textV
    }()
    
}

// MARK: - Private
extension YBOneClickLoginView {
    
    private func reloadTipsTextAttr() -> NSMutableAttributedString {
        let str: NSString = "使用本机号码一键登录即代表已同意《云游戏服务协议》和《中国电信天翼账号服务条款》并使用本机号码登录" as NSString
        let attrText = NSMutableAttributedString(string: str as String)
        let range0 = str.range(of: "使用本机号码一键登录即代表已同意")
        let range1 = str.range(of: "和")
        let range2 = str.range(of: "并使用本机号码登录")
        let rangeLink0 = str.range(of: "《云游戏服务协议》")
        let rangeLink1 = str.range(of: "《中国电信天翼账号服务条款》")
        attrText.addAttributes([.foregroundColor: UIColor.lightBlack58DarkLight230, .link: "agreement://", .font: UIFont.systemFont(ofSize: 12, weight: .semibold)], range: rangeLink0)
        attrText.addAttributes([.foregroundColor: UIColor.lightBlack58DarkLight230, .link: "accountService://", .font: UIFont.systemFont(ofSize: 12, weight: .semibold)], range: rangeLink1)
        attrText.addAttributes([.foregroundColor: ColorDef.gray153, .font: UIFont.systemFont(ofSize: 12)], range: range0)
        attrText.addAttributes([.foregroundColor: ColorDef.gray153, .font: UIFont.systemFont(ofSize: 12)], range: range1)
        attrText.addAttributes([.foregroundColor: ColorDef.gray153, .font: UIFont.systemFont(ofSize: 12)], range: range2)
        return attrText
    }
    
}

// MARK: - UITextViewDelegate代理
extension YBOneClickLoginView: UITextViewDelegate {
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        guard let scheme = URL.scheme else { return true }
        if scheme.contains("agreement") {
            let url = "https://www.baidu.com"
            let vc = BKWebViewVC(with: url)
            VisibleNaviCtrl?.pushViewController(vc)
            return false
        } else if scheme.contains("accountService") {
            let url = "https://www.baidu.com"
            let vc = BKWebViewVC(with: url)
            VisibleNaviCtrl?.pushViewController(vc)
            return false
        }
        return true
    }
}
